package com.hua.nailcampus.acl.controller;

import com.hua.nailcampus.acl.model.Messages;
import com.hua.nailcampus.acl.model.vo.UserDateVo;
import com.hua.nailcampus.acl.model.vo.MessagesVo;
import com.hua.nailcampus.acl.service.MessagesService;
import com.hua.nailcampus.utils.JwtHelper;
import com.hua.nailcampus.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:chenjianhua
 * @Date:2023/11/23 21:57
 * @Version 1.8
 */
@RestController
@Slf4j
@RequestMapping("nailCampus/message")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 获取聊天记录
     * @param request
     * @param toUserId
     * @return
     */
    @GetMapping("/getAllChat/{toUserId}")
    public Result getAllChat(HttpServletRequest request,
                             @PathVariable Integer toUserId){
        String token = request.getHeader("token");
        Integer fromUserId = JwtHelper.getUserId(token);
        List<Messages> messagesList = messagesService.getAllChat(fromUserId,toUserId);
        return Result.ok(messagesList);
    }

    /**
     * 获取首页消息(显示最近时间的一条消息)
     * @param request
     * @return
     */
    @GetMapping("/getUnreadMessages")
    public Result getUnreadMessages(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        List<MessagesVo> messagesVoList =  messagesService.getUnreadMessages(userId);
        return Result.ok(messagesVoList);
    }

    /**
     * 手动清除所有消息记录或者删除好友时清除所有消息记录
     * @param request
     * @param fId
     * @return
     */
    @GetMapping("/removeMessagesById/{fId}")
    public Result removeMessagesById(HttpServletRequest request,
                                 @PathVariable Integer fId){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        Boolean result = messagesService.removeMessagesById(userId,fId);
        if(result){
            return Result.ok("清空成功");
        }
        return Result.fail("清空失败");
    }

    /**
     *设置所有消息为已读
     * @param request 请求头token
     * @param fId   好友ID
     * @return
     */
    @GetMapping ("acceptMessage/{fId}")
    public Result acceptMessage(HttpServletRequest request,
                                 @PathVariable Integer fId){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        messagesService.acceptMessage(userId,fId);
        return Result.ok("成功");
    }

    /**
     * 根据关键字获取消息记录
     * @param request
     * @param fId
     * @param keyWord
     * @return
     */
    @GetMapping("getMessageByKeyWord/{fId}/{keyWord}")
    public Result getMessageByKeyWord(HttpServletRequest request,
                                      @PathVariable Integer fId,
                                      @PathVariable String keyWord){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        List<MessagesVo> messagesVoList = messagesService.getMessageByKeyWord(userId,fId,keyWord);
        return Result.ok(messagesVoList);
    }

    @PostMapping("/getMessageByDate")
    public Result getMessageByDate(@RequestBody UserDateVo userDate){
        List<MessagesVo> messagesVoList = new ArrayList<>();
        if(userDate.getSenderId()==null){
            messagesVoList = messagesService.getMessageByDate(userDate.getUserId(),userDate.getFriendId(),userDate.getStarDate(),userDate.getEndDate());
        }else{
            messagesVoList = messagesService.getMessageByFromID(userDate);
        }
        return Result.ok(messagesVoList);
    }

    @PostMapping ("/getMessageByFromID")
    public Result getMessageByFromID(@RequestBody UserDateVo userDateVo){
        log.info("userDateVo:{}",userDateVo);
        List<MessagesVo> messagesVoList = messagesService.getMessageByFromID(userDateVo);
        return Result.ok(messagesVoList);
    }

}
